//
//  WSNewsSelectionTableCell.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/23.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSNewsSelectionTableCell.h"

#import "WSNewsInfo.h"

@interface WSNewsSelectionTableCell ()
@property (nonatomic, strong) UIImageView *leftImage;
@property (nonatomic, strong) UIImageView *eyeIcon;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) UILabel *timeLabel;

@end

@implementation WSNewsSelectionTableCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *separator = [[UIView alloc] init];
    separator.backgroundColor = kSeparatorColor;
    [self.contentView addSubview:separator];
    
    self.leftImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.leftImage];
    
    self.eyeIcon = [[UIImageView alloc] init];
    [self.contentView addSubview:self.eyeIcon];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = WSFontMake(14);
    self.titleLabel.textColor = kFontColorBlack;
    [self.contentView addSubview:self.titleLabel];
    
    self.detailLabel = [[UILabel alloc] init];
    self.detailLabel.font = WSFontMake(12);
    self.detailLabel.textColor = COLOR_HEX(0x555555);
    self.detailLabel.numberOfLines = 2;
    [self.contentView addSubview:self.detailLabel];
    
    self.countLabel = [[UILabel alloc] init];
    self.countLabel.font = WSFontMake(13);
    self.countLabel.textColor = kFontColorGray;
    [self.contentView addSubview:self.countLabel];
    
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = WSFontMake(11);
    self.timeLabel.textColor = kFontColorGray;
    [self.contentView addSubview:self.timeLabel];
    
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.height.offset(0.5);
        make.bottom.offset(0.f);
    }];
    
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10.f);
        make.top.offset(10.f);
        make.size.mas_equalTo(CGSizeMake(102.f, 82.f));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftImage.mas_right).offset(15.f);
        make.right.offset(-10.f);
        make.top.equalTo(self.leftImage).offset(1.f);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftImage.mas_right).offset(10.f);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(12.f);
        make.right.offset(-10.f);
    }];
    
    [self.eyeIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10.f);
        make.top.equalTo(self.leftImage.mas_bottom).offset(15.f);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.eyeIcon.mas_right).offset(5.f);
        make.centerY.equalTo(self.eyeIcon);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10.f);
        make.centerY.equalTo(self.eyeIcon);
    }];
}

#pragma mark - Setter

- (void)setNewsInfo:(WSNewsInfo *)newsInfo {
    _newsInfo = newsInfo;
    [self.leftImage imageWithURL:newsInfo.picture defaultImage:kPlaceHolderImage];
    [self.eyeIcon setImage:WSImageMake(@"icon_eyes_selected")];
    self.titleLabel.text = newsInfo.title;
    NSString *str = [NSString stringWithFormat:@"【%@】%@",newsInfo.name, newsInfo.origin];
    [self.detailLabel addAttributeText:str attrs:@{NSForegroundColorAttributeName: COLOR_HEX(0x555555),
                                                  NSFontAttributeName: WSFontMake(12)
                                                  } range:NSMakeRange(0, newsInfo.name.length+2)];
    self.countLabel.text = [NSString stringWithFormat:@"(%@)",@(newsInfo.view)];
    self.timeLabel.text = newsInfo.create_at;
}

@end
